<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>小说</title>
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
</head>
<body>
<ul class="layui-nav " style="text-align: center ;" lay-filter="">
    <li class="layui-nav-item layui-this"><a href="novel_type.html">小说类型</a></li>
    <li class="layui-nav-item"><a href="novel_tag.html">小说标签</a></li>
    <li class="layui-nav-item"><a href="novel.html">小说详情</a></li>
    <li class="layui-nav-item"><a href="novel_chapter.html">小说章节</a></li>
</ul>
<form class="layui-form" action="">
    <div class="layui-form-item" style="margin: 10px; text-align: right;">
        <div class="layui-inline">
            <label class="layui-form-label">名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="novelName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">作者：</label>
            <div class="layui-input-inline">
                <input type="text" name="novelAuthor" autocomplete="off" class="layui-input">
            </div>
        </div>
        <button class="layui-btn layui-btn-md layui-btn-normal" lay-submit lay-filter="queryNovel">查询</button>
    </div>
</form>

<table id="demo" class="layui-table" lay-data="{skin:'line', even:true, size:'sm'}" lay-filter="test"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="read">阅读</a>
</script>

<script src="layui/layui.js"></script>
<script src="js/common.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;

        table.render({
            elem: '#demo'
            , height: 312
            , url: BASE_URL + '/novel/list' //数据接口
            , request: {
                pageName: 'pageNum'
                , limitName: 'pageSize'
            }
            , parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": (res.data !== undefined && res.data.total !== undefined) ? res.data.total : 0,
                    "data": (res.data !== undefined && res.data.list !== undefined) ? res.data.list : {},
                }
            }
            , page: true //开启分页
            , limits: [10, 15, 30, 50, 100, 200]
            , height: $(document).height() - $('#demo').offset().bottom - 10
            , defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'exports', 'print']
            , cols: [[ //表头
                {field: 'novelId', title: 'ID', width: 200, hide: true, sort: true, fixed: 'left'}
                , {field: 'novelName', title: '名称', width: 300}
                , {field: 'novelAuthor', title: '作者', width: 150, sort: true}
                , {field: 'novelWords', title: '字数', width: 150}
                , {field: 'novelSynopsis', title: '简介', width: 600}
                , {field: 'detailUrl', title: 'url', hide: true, width: 150}
                , {
                    field: 'status', title: '状态', width: 150, sort: true
                    , templet: function (d) {
                        return d.status == 0 ? "连载中" : "已完结";
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]

        });

        form.on('submit(queryNovel)', function (data) {
            var param = data.field;

            table.reload('demo', {
                page: {
                    curr: 1
                }
                , where: {
                    novelName: param.novelName
                    , novelAuthor: param.novelAuthor
                }
            });
            return false;
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'read') {
                layer.open({
                    context: data.novelName
                    , success: function (layer, index) {
                        self.location = 'read.html?novelId=' + data.novelId
                    }
                })
            }
        });
    });

</script>
</body>
</html>